package cn.et.model;

import java.util.List;
import java.util.Map;

import cn.et.model.Dbuit;
import cn.et.model.PageTools;

public class Desk {
	
	public Integer getTableCount(String name) throws Exception{
		if(name==null){
			name="";
		}
		String sql="select count(rowid) as cr from DESK where dname like '%"+name+"%'";
		List<Map> li=Dbuit.query(sql);
		return Integer.parseInt(li.get(0).get("CR").toString());
	}
	
	public PageTools getTablePage(String name,Integer curPage) throws Exception{
		if(name==null){
			name="";
		}
		Integer totalCount =getTableCount(name);
		PageTools pt= new PageTools(curPage,totalCount,null);
		StringBuffer sf=new StringBuffer();
		List<Map> result =Dbuit.query("select * from (select t.*,rownum rn from DESK t where t.dname like '%"+name+"%')"
				+ " where rn>="+pt.getStartIndex()+"and rn<="+pt.getEndIndex());
		pt.setData(result);
		return pt;
	}
	public void saveDesk(String deskName) throws Exception{
		int i= getTableCount(null);
		String sql;
		if(i==0){
			sql="insert into desk values(1,'"+deskName+"',0,null)";
			Dbuit.execute(sql);
		}else{
			sql="insert into desk values((select max(deskid)+1 from desk),'"+deskName+"',0,null)";
			Dbuit.execute(sql);
		}

	}
	public void deleteDesk(String deskId) throws Exception{
		String sql="delete from desk where deskid="+deskId;
		Dbuit.execute(sql);
	}
}
